package cn.pugle.base;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tzp on 15-11-10.<br/>
 * 数字格式化
 * 进制转换
 * ip转换
 * int
 * asList has bug!!
 */
public class FormatTest {
    public static void main(String[] args) {
//        System.out.println("c847c9a6bf1f3801757775f1b283b0f2".toUpperCase());
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        System.out.println(df.format(1.12645));

        //62进制转换
        /*final char[] CA = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
        int result = 0;
        String originDigtal = "6wtLD";
        for (int i = 0; i < originDigtal.length(); i++) {
            char c = originDigtal.charAt(i);
            int i1 = Arrays.binarySearch(CA, c);//ou shit binarySearch
            result += i1 * Math.pow(62, originDigtal.length() - i - 1);
        }
        System.out.println(result);*/

        System.out.println(parseDotSepaIP("10.200.10.0"));
        //about int
        System.out.println(Integer.MAX_VALUE);
        System.out.println(1 << 31);

/*        String s = "";
        String[] split = s.split("\n");
        Arrays.sort(split);
        for (int i = 0; i < split.length; i++) {
            int x = parse62To10(split[i]);
            System.out.print(x + ",");
        }*/
        System.out.println("62 to 10: " + parse62To10("6qoFx"));
        System.out.println("10 to 62: " + parse10To62(104880564));
    }

    static String parseDotSepaIP(String dotSepaIP) {
        long[] ip = new long[4];
        // 先找到IP地址字符串中.的位置
        int position1 = dotSepaIP.indexOf(".");
        int position2 = dotSepaIP.indexOf(".", position1 + 1);
        int position3 = dotSepaIP.indexOf(".", position2 + 1);
        // 将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(dotSepaIP.substring(0, position1));
        ip[1] = Long.parseLong(dotSepaIP.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(dotSepaIP.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(dotSepaIP.substring(position3 + 1));
        Long ipLong = (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
        return ipLong + "";
    }

    public static int parse62To10(String originDigtal) {
        List<Character> CL = new ArrayList<>();
        char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
        for (char c : chars) {
            CL.add(c);
        }
        int result = 0;
        for (int i = 0; i < originDigtal.length(); i++) {
            char c = originDigtal.charAt(i);
            int i1 = CL.indexOf(c);
            result += i1 * Math.pow(62, originDigtal.length() - i - 1);
        }
        return result;
    }

    public static String parse10To62(int digit) {
        String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        String result = "";
        int di = digit;
        while (di>0) {
            int i = di % 62;
            result = s.charAt(i) + result;
            di = di / 62;
        }
        return result;
    }
    public static void DighitFormat() {
        System.out.printf("%03d", 01);
        System.out.printf("%3d", 01);
    }
}
